package com.runa.rccprotocol.dto.HuiZhongOne;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;

public class HuiZhongOneRequestData extends ConcentratorRequestData {

    public HuiZhongOneRequestData(String address) {
        super(address);
    }

    private byte[] requestData;


    public void readHouseValve(Integer valveIndex) {
        readHeatMeter(valveIndex);
    }

    public void readHeatMeter(Integer meterIndex) {
//        requestData = new byte[]{(byte) 0xFE, (byte) 0xFE, (byte) 0xFE, (byte) 0xFE,
//                0x68,0x20,
//                0,0,0,0,
//                0x0,0x11,0x11,0x01,0x03, 0x1F, (byte) 0x90,0x00,
//                0,
//                0x16};
        requestData = new byte[]{(byte) 0x68, (byte) 0x2F, (byte) 0x00, (byte) 0x2F, (byte) 0x00,
                0x68, 0x4B, 0, 0, 0, 0, 0x02,
                0x0C,0x70,0x05,
                0x00,0x00,
                0x00,0x16};
        ByteUtil.tenstringToBcd(requestData, address, 7, false);
        String format = String.format("%x", meterIndex);
        int i = Integer.parseInt(format, 16);
        requestData[15] = (byte) (i & 0xff);
        requestData[16] = (byte) (i >> 8 & 0xff);

        requestData[17] = ByteUtil.sum(requestData, 6, requestData.length-2-6);
//        requestData[requestData.length-2] = ByteUtil.sum(requestData, 6, requestData.length-2-6);
        setHaveResult(true);
    }

    public void valveControlExecuteStatus(Integer valveIndex, Integer open) {
        requestData = new byte[]{(byte) 0x68, (byte) 0x37, (byte) 0x00, (byte) 0x37, (byte) 0x00,
                0x68, 0x4B, 0, 0, 0, 0, 0x02,
                0x0E,0x70,0x01,
                0x00, // 本次阀门控制数量
                0x00,0x00, // 阀门序号
                0x00, // 0: 开 1：关
                0x00,0x16};
    }

    public void valveControl(Integer valveIndex, Integer open) {
        requestData = new byte[]{(byte) 0x68, (byte) 0x37, (byte) 0x00, (byte) 0x37, (byte) 0x00,
                0x68, 0x4A, 0, 0, 0, 0, 0x02,
                0x05,0x70,0x02,
                0x00, // 本次阀门控制数量
                0x00,0x00, // 阀门序号
                0x00, // 0: 开 1：关
                0x00,0x16};
        ByteUtil.tenstringToBcd(requestData, address, 7, false);

        requestData[15] = (byte)0x01;

        requestData[16] = (byte) (valveIndex & 0xff);
        requestData[17] = (byte) (valveIndex >> 8 & 0xff);

        requestData[18] = (byte) (open & 0xff);

        requestData[19] = ByteUtil.sum(requestData, 6, requestData.length-2-6);
        setHaveResult(true);
    }


    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        return this.requestData;
    }

}
